﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpaceGame
{
    public abstract class PlayerSpaceShip : SpaceShip
    {
        protected bool left;
        protected bool right;
        protected bool top;
        protected bool down;

        public static int MaxX { get; set; }
        public static int MaxY { get; set; }

        public PlayerSpaceShip(Position topLeft, char[,] image)
            :base(topLeft, image, 1, new Position(0,0))
        {
        }

        public override void Update()
        {
            base.Update();

            if (this.TopLeft.Col > MaxX)
            {
                this.TopLeft = new Position(0, this.TopLeft.Row);
            }

            //this.UpdatePosition();
            //this.image = this.NextFrame;
        }

        public override void CollisionResponse(ICollidable instance)
        {
            if (!(instance is Bullet))
            {
                this.IsDestroyed = true;
            }
        }

        public void MoveLeft()
        {
            this.left = true;
        }
        public void MoveRight()
        {
            this.right = true;
        }
        public void MoveUp()
        {
            this.top = true;
        }
        public void MoveDown()
        {
            this.down = true;
        }

        protected void RestrainMovement()
        {
            Position newPosition = this.TopLeft += this.Direction;

            // check horizontal
            if (newPosition.Col + this.Image.GetLength(1) >= MaxX)
            {
                newPosition.Col = MaxX - this.Image.GetLength(1) - 1;
            }

            if (newPosition.Col <= 0)
            {
                newPosition.Col = 0;
            }

            //check vertical
            if (newPosition.Row + this.Image.GetLength(0) >= MaxY + 4)      // some mess-up with max rows
            {
                newPosition.Row = MaxY - this.Image.GetLength(0) - 1;
            }

            if (newPosition.Row < 0)
            {
                newPosition.Row = 0;
            }

            this.TopLeft = newPosition;
        }

    }
}
